<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'jh:plan:jhPlanArea:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.cid }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsJhPlanJhPlanAreaList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { jhPlanAreaDelete, jhPlanAreaListData } from '/@/api/wms/jh/plan/jhPlanArea';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('jh.plan.jhPlanArea');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('备货货位管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('cid'),
        field: 'cid',
        component: 'Input',
      },
      {
        label: t('发货单号'),
        field: 'fbillno',
        component: 'Input',
      },
      {
        label: t('订单号'),
        field: 'forderno',
        component: 'Input',
      },
      {
        label: t('购货单位内码'),
        field: 'fsupplyid',
        component: 'Input',
      },
      {
        label: t('购货单位编码'),
        field: 'fsupplierno',
        component: 'Input',
      },
      {
        label: t('购货单位名称'),
        field: 'fsuppliername',
        component: 'Input',
      },
      {
        label: t('商品'),
        field: 'inv_code',
        component: 'Input',
      },
      {
        label: t('货位编码'),
        field: 'cpos_code',
        component: 'Input',
      },
      {
        label: t('货位名称'),
        field: 'cpos_name',
        component: 'Input',
      },
      {
        label: t('数量'),
        field: 'fqty',
        component: 'Input',
      },
      {
        label: t('形象刊'),
        field: 'cfree1',
        component: 'Input',
      },
      {
        label: t('自由项2'),
        field: 'cfree2',
        component: 'Input',
      },
      {
        label: t('自由项3'),
        field: 'cfree3',
        component: 'Input',
      },
      {
        label: t('公司编码'),
        field: 'company_code',
        component: 'Input',
      },
      {
        label: t('公司名称'),
        field: 'company_name',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('创建人名称'),
        field: 'create_by_name',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('cid'),
      dataIndex: 'cid',
      key: 'a.cid',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('发货单号'),
      dataIndex: 'fbillno',
      key: 'a.fbillno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('订单号'),
      dataIndex: 'forderno',
      key: 'a.forderno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('购货单位内码'),
      dataIndex: 'fsupplyid',
      key: 'a.fsupplyid',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('购货单位编码'),
      dataIndex: 'fsupplierno',
      key: 'a.fsupplierno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('购货单位名称'),
      dataIndex: 'fsuppliername',
      key: 'a.fsuppliername',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('商品'),
      dataIndex: 'inv_code',
      key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('货位编码'),
      dataIndex: 'cpos_code',
      key: 'a.cpos_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('货位名称'),
      dataIndex: 'cpos_name',
      key: 'a.cpos_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'fqty',
      key: 'a.fqty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('形象刊'),
      dataIndex: 'cfree1',
      key: 'a.cfree1',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自由项2'),
      dataIndex: 'cfree2',
      key: 'a.cfree2',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自由项3'),
      dataIndex: 'cfree3',
      key: 'a.cfree3',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('公司编码'),
      dataIndex: 'company_code',
      key: 'a.company_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('公司名称'),
      dataIndex: 'company_name',
      key: 'a.company_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('创建人名称'),
      dataIndex: 'create_by_name',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('修改时间'),
      dataIndex: 'update_date',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑备货货位'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'jh:plan:jhPlanArea:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除备货货位'),
        popConfirm: {
          title: t('是否确认删除备货货位'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'jh:plan:jhPlanArea:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: jhPlanAreaListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await jhPlanAreaDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
